#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update:

'''docstring
'''
__revision__ = '0.1'
__author__ = 'lxd'

import wx
import config
import tsina, tdouban, tjavaeye

class JavaeyeDialog(wx.Dialog):
    def __init__(self, parent, log, name, **kwargs):
        wx.Dialog.__init__(self, parent, -1, name, **kwargs)
        self.log = log
        self.lblName = wx.StaticText(self, -1, u'用户名')
        self.txtName = wx.TextCtrl(self, -1, '')  
        self.lblPwd = wx.StaticText(self, -1, u'密码')
        self.txtPwd = wx.TextCtrl(self, -1, '', style=wx.TE_PASSWORD)  
        
        self.btnOk = wx.Button(self, wx.ID_OK, u'确定')
        self.btnCanel = wx.Button(self, wx.ID_CANCEL, u'取消')     
        self.layout()
        
    def layout(self):
        self.Center()
        titleSizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
        titleSizer.AddGrowableCol(1)
        titleSizer.Add(self.lblName, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        titleSizer.Add(self.txtName, 0, wx.EXPAND)
        titleSizer.Add(self.lblPwd, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        titleSizer.Add(self.txtPwd, 0, wx.EXPAND)

        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer.Add(self.btnOk, 0, wx.ALL, 5)
        btnSizer.Add(self.btnCanel, 0, wx.ALL, 5)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(titleSizer, 0, wx.ALL, 5)
        mainSizer.Add(btnSizer, 0, wx.ALL, 5)
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)

class MainPanel(wx.Panel):
    def __init__(self, parent, log, **kargs):
        wx.Panel.__init__(self, parent, **kargs)
        self.log = log
        self.lblType = wx.StaticText(self, -1, u'发送类型')
        self.chkSina = wx.CheckBox(self, -1, u'新浪微博')
        self.chkDouban = wx.CheckBox(self, -1, u'豆瓣广播')
        self.chkJavaeye = wx.CheckBox(self, -1, u'javaeye闲聊')
        self.Bind(wx.EVT_CHECKBOX, self.OnJavaeyeClick, self.chkJavaeye)

        self.lblWord = wx.StaticText(self, -1, u'发送消息')
        self.txtWord= wx.TextCtrl(self, -1, '', size=(200, 100), style=wx.TE_MULTILINE)  
        self.txtWord.SetFocus()
        
        self.btnOk= wx.Button(self, -1, u'发送')
        self.Bind(wx.EVT_BUTTON, self.OnOkClick, self.btnOk)  
        self.btnOk.SetDefault()
        
        self.layout()
        self.init()

    def init(self):
        self.user = config.loadUserData()
        self.chkSina.SetValue(self.user['chkSina'])
        self.chkDouban.SetValue(self.user['chkDouban'])
        self.chkJavaeye.SetValue(self.user['chkJavaeye'])
        self.getUserInfo()
        
    def getUserInfo(self):
        self.user = config.loadUserData()
        self.sina_token_key = self.user['sina_token_key']
        self.sina_token_secret = self.user['sina_token_secret']
        self.douban_token_key = self.user['douban_token_key']
        self.douban_token_secret = self.user['douban_token_secret']
        self.javaeye_name = self.user['javaeye_name']
        self.javaeye_pwd = self.user['javaeye_pwd']

    def layout(self):
        self.Center()
        chkSizer = wx.BoxSizer(wx.HORIZONTAL)
        chkSizer.Add(self.chkSina, 0, wx.ALL, 5)
        chkSizer.Add(self.chkDouban, 0, wx.ALL, 5)
        chkSizer.Add(self.chkJavaeye, 0, wx.ALL, 5)

        infoSizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
        infoSizer.AddGrowableCol(1)
        infoSizer.Add(self.lblType, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(chkSizer, 0, wx.EXPAND)
        infoSizer.Add(self.lblWord, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        infoSizer.Add(self.txtWord, 1, wx.EXPAND)
        
        topBox = wx.StaticBox(self, -1, u'一键发微博')
        topSizer = wx.StaticBoxSizer(topBox, wx.VERTICAL)
        topSizer.Add(infoSizer, 1, wx.EXPAND|wx.ALL, 5)
        
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(topSizer, 0, wx.EXPAND|wx.ALL, 5)
        mainSizer.Add(self.btnOk, 0, wx.ALIGN_CENTRE|wx.TOP|wx.BOTTOM, 20)
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)

    def OnOkClick(self, event): 
        word = self.txtWord.GetValue().strip()
        if not word:
            print 'no word'
        else:
            self.getUserInfo()
            if self.chkSina.GetValue(): self.sinaAction(word)
            if self.chkDouban.GetValue(): self.doubanAction(word)
            if self.chkJavaeye.GetValue(): self.javaeyeAction(word)

    def OnJavaeyeClick(self, event):
        if self.chkJavaeye.GetValue():
            dialog = JavaeyeDialog(self, self.log, 'javaeye', size = (200, 200))
            dialog.txtName.SetValue(self.javaeye_name)
            dialog.txtPwd.SetValue(self.javaeye_pwd)
            result = dialog.ShowModal()
            if result  ==  wx.ID_OK:
                self.javaeye_name = dialog.txtName.GetValue().strip()
                self.javaeye_pwd = dialog.txtPwd.GetValue().strip()
                config.saveUserData(javaeye_name = self.javaeye_name, javaeye_pwd = self.javaeye_pwd)


    def sinaAction(self, word):
        sina = tsina.Sina(config.sina_consumer_key, config.sina_consumer_secret)
        if not self.sina_token_key:
            key, secret = sina.auth()
            config.saveUserData(sina_token_key = key, sina_token_secret = secret)
        else:
            sina.setToken(self.sina_token_key, self.sina_token_secret)
        r = sina.write(word)
        if r:
            print 'sina ok'

    def doubanAction(self, word):
        d = tdouban.Douban(config.douban_consumer_key, config.douban_consumer_secret)
        if not self.douban_token_key:
            r = d.auth()
            if r:
                key, secret = r
                config.saveUserData(douban_token_key = key, douban_token_secret = secret)
                d.setToken(key, secret)
            else:
                print 'douban auth error'
        else:
            d.setToken(self.douban_token_key, self.douban_token_secret)
        r = d.write(word)
        if r:
            print 'douban ok'

    def javaeyeAction(self, word):
        if self.javaeye_name and self.javaeye_pwd:
            javaeye = tjavaeye.Javaeye(self.javaeye_name, self.javaeye_pwd)
            r = javaeye.write(word)
            if r:
                print 'javaeye ok'
        else:
            print u'请先设置登录名和密码'

class MainFrame(wx.Frame):
    def __init__(self,log, name, **kargs):
        wx.Frame.__init__(self, None, -1, name, **kargs)
        self.log = log
        self.mainPanel = MainPanel(self, self.log)
        self.Center()
        self.Bind(wx.EVT_CLOSE, self.OnClose)  

    def OnClose(self, event):
        chkSina = self.mainPanel.chkSina.GetValue()
        chkDouban = self.mainPanel.chkDouban.GetValue()
        chkJavaeye = self.mainPanel.chkJavaeye.GetValue()
        config.saveUserData(chkSina = chkSina, chkDouban = chkDouban, chkJavaeye = chkJavaeye)
        event.Skip()

if __name__ == '__main__':
    import logging
    app = wx.PySimpleApp()
    MainFrame(logging, u'一键发微博', size = (400, 300)).Show()
    app.MainLoop()
